package makeChanges.controller;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.DatabaseException;
import model.Item;
import model.User;
import model.Utilities;

/**
 * Servlet implementation class ChooseProduct
 */
@WebServlet("/ChooseProduct")
public class ChooseProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("S")){
			response.sendRedirect("Login.html");
			return;
		}
		request.setCharacterEncoding("UTF-8");
		String itemId=request.getParameter("id");
		String description = new String(request.getParameter("description").getBytes(
                "iso-8859-1"), "UTF-8");
		String edit = (String) session.getAttribute("edit");
		if(edit.equals("delete")){
			try {
				Utilities.deleteProduct(itemId);
				response.sendRedirect("/Store/Supplier.html");
			} catch (DatabaseException e) {
				e.printStackTrace();
				response.sendRedirect("/Store/TechnicalDifficulties.html");
			}
		}else if(edit.equals("counts")){
			try {
				Optional<Item> itemOptional = Item.getItem(Integer.parseInt(itemId),0);
					Item item = itemOptional.get();
					request.setAttribute("item", item);
					RequestDispatcher dispatcher = request.getRequestDispatcher("/MakeChanges/ChangeCounts.jsp");
					dispatcher.forward(request, response);
			} catch (DatabaseException e) {
				e.printStackTrace();
				response.sendRedirect("/Store/TechnicalDifficulties.html");
			}
			
		}else if(edit.equals("categorize")){
			try {
				Map<Integer,String> idToCategoryDescriptionMap = Utilities.getIdToCategoryDescriptionMap();
				Set<Integer> productCategoryIds = Utilities.getProductCategoryIds(itemId);
				request.setAttribute("idToCategoryDescriptionMap", idToCategoryDescriptionMap);
				request.setAttribute("productCategoryIds", productCategoryIds);
				request.setAttribute("description",description);
				request.setAttribute("itemId", itemId);
				RequestDispatcher dispatcher = request.getRequestDispatcher("/MakeChanges/Categorize.jsp");
				dispatcher.forward(request, response);
			} catch (DatabaseException e) {
				e.printStackTrace();
				response.sendRedirect("/Store/TechnicalDifficulties.html");
			}
		}
				
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
